<template>
  <div id="cornerComment" v-if="dialogVisible">
    <div class="cornerComment-in" ref="dialogBox">
      <div class="cornerComment-in-title">
        <span>添加/编辑教职工</span>
        <span class="el-icon-circle-close close-Btn-line" @click="closePOpup"></span>
      </div>
      <div class="commentBody">
        <el-form :model="editForm" label-width="100px" :rules="editFormRules" ref="editForm">
          <el-form-item label="头像:" prop="pic">
            <!--            <PictureUpload @getChildValue="getChildValue" imgNumber="1" page="teachingPic"-->
            <!--                           :picSrc="editForm.pic"></PictureUpload>-->
            <imgUploadModle @getChildValue="getChildValue" imgNumber="1" page="teachingPic"
                            :picSrc="editForm.pic"></imgUploadModle>
          </el-form-item>
          <el-form-item label="账户名:" prop="account">
            <el-input v-model="editForm.account" auto-complete="off" placeholder="填写账户名"></el-input>
            （用于登入后台账号）
          </el-form-item>
          <el-form-item label="姓名:" prop="realname">
            <el-input v-model="editForm.realname" auto-complete="off" placeholder="填写姓名"></el-input>
          </el-form-item>
          <el-form-item label="职工归属:">
            <el-radio-group v-model="editForm.school_id">
              <el-radio :label="item.id" v-for="item in schoolList">{{ item.name }}</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="班级教师:">
            <el-radio-group v-model="editForm.is_teacher" @change="userTypeFn()">
              <el-radio :label="0">否</el-radio>
              <el-radio :label="1">是</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="班级身份:" v-if="editForm.is_teacher==1">
            <el-radio-group v-model="editForm.type">
              <el-radio :label="0">班主任</el-radio>
              <el-radio :label="1">带班老师</el-radio>
              <el-radio :label="2">其他（如:生活老师，体育老师等）</el-radio>
            </el-radio-group>
          </el-form-item>
          <div>
            <el-form-item label="所在区域:" prop="section_id">
<!--              <div style="display: flex">-->
<!--                <sectionInput @changeSection="changeSection" :sectionId.sync="editForm.section_id"></sectionInput>-->
<!--                <classInput @changeClass="changeClass" :sectionId.sync="editForm.section_id"-->
<!--                            :classId.sync="editForm.class_id"></classInput>-->
<!--              </div>-->
              <chooseClass @onChangeClass="onChangeClass"
                           :section_id="editForm.section_id || 0"
                           :class_id="editForm.class_id || 0" v-if="dialogVisible && loadingStatus"></chooseClass>
            </el-form-item>
          </div>
          <div>
            <el-form-item label="管理区域:" prop="section_id">
              <el-button @click="chooseAreaFn" size="small">选择区域</el-button>
              <span v-for="item in areaOption">【{{ item.class_name }}】</span>
            </el-form-item>
          </div>
          <el-form-item label="论坛管理:">
            <el-radio-group v-model="editForm.is_forum">
              <el-radio :label="0">否</el-radio>
              <el-radio :label="1">是</el-radio>
            </el-radio-group>
            <span class="ml-30">(是否查看论坛所有文章)</span>
          </el-form-item>
          <el-form-item label="开放权限:">
            <el-radio-group v-model="editForm.is_all" @change="radioAllchange">
              <el-radio :label="0">否</el-radio>
              <el-radio :label="1">是</el-radio>
            </el-radio-group>
            <span class="ml-30">(是否查看系统所有数据)</span>
          </el-form-item>
          <el-form-item label="参与考勤:">
            <el-radio-group v-model="editForm.is_sign" @change="radioAllchange">
              <el-radio :label="0">否</el-radio>
              <el-radio :label="1">是</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="手机签到:">
            <el-radio-group v-model="editForm.is_sign_all">
              <el-radio :label="0">不允许</el-radio>
              <el-radio :label="1">允许</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-row>
            <el-col :span="8">
              <el-form-item label="角色:" prop="role">
                <el-select v-model="editForm.role" placeholder="请选择用户身份">
                  <el-option :label="item.name" :value="item.id" v-for="(item, index) in role" :key="index"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
            </el-col>
            <el-col :span="8"></el-col>
          </el-row>
        </el-form>
      </div>
      <div class="cornerComment-in-footer">
        <el-button @click="closePOpup" size="small">取消</el-button>
        <el-button @click="submitForm('editForm')" size="small" type="primary">确定并保存</el-button>
      </div>
    </div>
    <MultipleClassModle :visible.sync="classAreaState" :dynamicTagsArray="dynamicTagsArray"
                        @studentFn="studentFn"></MultipleClassModle>
  </div>
</template>

<script>
import classInput from '@/components/template/classInput.vue'
import sectionInput from '@/components/template/sectionInput.vue'
import {schoolIndex} from '@/api/admin/system'

export default {
  props: ['visible', 'tearchOption'], // commoent 学生点评 collection 学生采集
  components: {
    classInput,
    sectionInput
  },
  data() {
    return {
      dialogVisible: false,
      classAreaState: false,
      dynamicTagsArray: {},
      editForm: {
        pic: '',
        account: '',
        class_id: '',
        realname: '',
        section_id: '',
        role: '',
        type: 1,
        is_teacher: 0, // 是否是教师
        is_forum: 0,
        is_sign: 1,
        is_sign_all: 0,
        is_all: 0, // 1开 0关
        public_ids: '',
        school_id: 0
      },
      usertype: '1',
      // 编辑界面数据
      editFormRules: {
        account: [
          {required: true, message: '请填写账户名', trigger: 'blur'}
        ],
        realname: [
          {required: true, message: '请填写姓名', trigger: 'blur'}
        ],
        role: [
          {required: true, message: '请选择身份', trigger: 'blur'}
        ]
      },
      role: [],
      rolePage: 1,
      classesvalue: '',
      flag: true,
      areaOption: [],
      schoolList: [],
      loadingStatus: false
    }
  },
  watch: {
    visible() {
      this.dialogVisible = this.visible
      if (this.visible) {
        if (this.tearchOption) {
          this.InitFn()
        }else{
          this.loadingStatus = true
        }
        this.roleFn()
        this.schoolIndexFn()
      } else {
        this.loadingStatus = false
        this.editForm = {
          pic: '',
          account: '',
          class_id: '',
          realname: '',
          section_id: '',
          school_id: 0,
          role: '',
          type: 1,
          is_forum: 0,
          is_sign: 1,
          is_sign_all: 0,
          is_teacher: 0, // 是否是教师
          is_all: 0 // 1开 0关
        }
      }
    }
  },
  created() {
  },
  methods: {
    schoolIndexFn() {
      schoolIndex().then(res => {
        let list_ = JSON.parse(JSON.stringify(res.list))
        list_.unshift({id: 0, name: '其他'})
        this.schoolList = list_
      })
    },
    chooseAreaFn() {
      let a_ = {}
      for (let item of this.areaOption) {
        a_[item.id] = item
      }
      this.dynamicTagsArray = JSON.parse(JSON.stringify(a_))
      this.classAreaState = true
    },
    studentFn(option) {
      console.log('option:', option)
      let a_ = []
      for (let index in option) {
        a_.push(option[index])
      }
      this.areaOption = JSON.parse(JSON.stringify(a_))
    },
    onChangeClass(option) {
      console.log('option', option)
      this.editForm.class_id = option.class_id
      this.editForm.section_id = option.section_id
    },
    InitFn() {
      console.log('请求详情')
      this.$fetch(this.api.admin_user_edit, {
        id: this.tearchOption.id
      }).then((response) => {
        if (response.code === 0) {
          this.sectionList = response.section
          let array_ = {}
          for (let i in response.class) {
            let new_array = array_[response.class[i].section_id] || []
            new_array.push(response.class[i])
            array_[response.class[i].section_id] = new_array
          }
          this.classesListNew = array_
          this.editForm.is_teacher = response.detail.is_teacher // 是否是教师
          this.editForm.account = response.detail.account
          this.editForm.realname = response.detail.realname
          this.editForm.role = response.detail.role.id
          this.editForm.is_all = response.detail.is_all
          this.editForm.pic = response.detail.pic
          this.editForm.type = response.detail.type
          this.editForm.is_sign = response.detail.is_sign
          this.editForm.is_sign_all = response.detail.is_sign_all
          this.editForm.school_id = response.detail.school_id
          this.areaOption = response.public_class || []
          if (response.detail.class_id) {
            this.editForm.class_id = response.detail.class_id
          }
          if (response.detail.section_id) {
            this.editForm.section_id = response.detail.section_id
            // this.sectionChange(response.detail.section_id, 'init')
          }
          console.log('编辑获取的数据内容：', this.editForm)
          this.loadingStatus = true
        } else {
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    submitForm(formName) {
      let that = this;
      let data_ = JSON.parse(JSON.stringify(this.editForm))
      console.log('data_:', data_)
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (!data_.pic) {
            that.$message.error('请选择上传用户头像')
            return
          }
          if (!data_.section_id) {
            that.$message.error('请选择年段')
            return
          }
          if (!data_.class_id) {
            that.$message.error('请选择班级区域')
            return
          }
          let a_ = []
          for (let item of this.areaOption) {
            a_.push(item.id)
          }
          this.editForm.public_ids = a_.join(',')
          this.submitFormAdd()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    userTypeFn() {
      console.log('身份变了：', this.editForm.is_teacher)
      if (this.editForm.is_teacher) {
        this.editForm.type = 1
      } else {
        this.editForm.type = 2
      }
    },
    async submitFormAdd() {
      let url = this.api.admin_user_add
      if (this.tearchOption) {
        url = this.api.admin_user_edit
        this.editForm.id = this.tearchOption.id
      }
      if (Number(this.usertype) === 0) {
        this.editForm.type = ''
      }
      let data = await this.$post(url, this.editForm);
      console.log('编辑成功：', data)
      if (!data.code) {
        this.$message.success('编辑成功')
        if (!this.tearchOption) {
          this.$refs['editForm'].resetFields()
        }
        this.$emit('InitFn')
        this.closePOpup()
      } else {
        this.$message.error(data.msg)
      }
    },
    // 角色
    async roleFn() {
      let data = await this.$post(this.api.role.select, {});
      if (!data.code) {
        for (let i in data.list) {
          this.role.push(data.list[i])
        }
      }
    },
    radioAllchange() {
      if (this.editForm.is_all === 1) {
        this.editForm.is_all = 0
        this.$confirm('此操作将数据全部展示于此用户, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.editForm.is_all = 1
        }).catch(() => {
        })
      }
    },
    changeSection(option) {
      this.editForm.class_id = ''
      this.editForm.section_id = option
    },
    changeClass(option) {
      this.editForm.class_id = option
    },
    getChildValue(option) {
      this.editForm.pic = option
    },
    // 关闭弹窗
    closePOpup() {
      this.$emit('update:visible', false)
    },
    getJson(option) {
      return JSON.parse(JSON.stringify(option))
    },
  },
  mounted() {
  }
}
</script>
<style scoped>
#cornerComment {
  height: 100vh;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, .5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9;
}

.cornerComment-in {
  width: 60%;
  height: 75%;
  background: #fff;
  display: flex;
  flex-direction: column;
  border-radius: 5px;
}

.cornerComment-in-title {
  height: 50px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ddd;
  font-size: 24px;
}

.cornerComment-in-title span {
  padding: 0 10px;
}

.cornerComment-in-title span:first-child {
  flex: 1;
  font-size: 16px;
}

.cornerComment-in-footer {
  height: 50px;
  border-top: 1px solid #ddd;
  display: flex;
  align-items: center;
  padding: 0 10px;
  justify-content: center;
}

.commentBody {
  padding: 20px;
  flex: 1;
  overflow-y: auto;
}

.close-Btn-line {
  cursor: pointer;
}

</style>


